package com.lanmei.group.weixin.controller.admin;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.lanmei.group.weixin.model.CmsArticle;
import com.lanmei.group.weixin.model.CmsCategory;
import com.lanmei.group.weixin.utils.RequestUtil;
import com.lanmei.group.weixin.utils.ShiroUtil;
@RequiresRoles(value = {"user" },logical=Logical.OR)
public class CmsArticleController extends Controller {

	public void list(){
		int siteId = RequestUtil.getSiteId(getSession());
		Page<CmsArticle> page = new CmsArticle().paginate(1, 1000, "select *", " from cms_article where site_id = "+siteId+" order by create_time desc");
		setAttr("page", page);
		render("list.jsp");
	}
	public void toAdd(){
		int siteId = RequestUtil.getSiteId(getSession());
		Page<CmsCategory> page =new  CmsCategory().paginatRootBySiteId(1, 100,siteId);
		initCmsCategory(page.getList());
		setAttr("page", page);
		render("add.jsp");
	}
	public void add(){
		CmsArticle article = getModel(CmsArticle.class, "article");
		String [] images = getParaValues("images");
		String imagesUrl = "";
		if(images!=null&&images.length>0){
			for (int i = 0; i < images.length; i++) {
				imagesUrl+=images[i]+",";
			}
		}
		int siteId = RequestUtil.getSiteId(getSession());
		article.setSiteId(siteId);
		article.setImages(imagesUrl);
		article.setPublishTime(new Date());
		article.setCreateTime(new Date());
		article.setCreateUser(ShiroUtil.getLoginName());
		article.save();
		this.redirect("/admin/cmsarticle/list");
	}
	public void toEdit(){
		int articleId = getParaToInt("articleId");
		CmsArticle article = CmsArticle.dao.findById(articleId);
		int siteId = RequestUtil.getSiteId(getSession());
		Page<CmsCategory> page =new  CmsCategory().paginatRootBySiteId(1, 100,siteId);
		initCmsCategory(page.getList());
		setAttr("page", page);
		setAttr("article", article);
		render("edit.jsp");
	}
	private List<CmsCategory> initCmsCategory(List<CmsCategory> list) {
		if(list.isEmpty()){
			return null;
		}
		for (CmsCategory cmsCategory : list) {
			Page<CmsCategory> page =new  CmsCategory().paginatByParentId(1, 100,cmsCategory.getCatId());
			cmsCategory.setChildCategory(initCmsCategory(page.getList()));
		}
		return list;
	}
	public void update(){
		CmsArticle article = getModel(CmsArticle.class, "article");
		article.setUpdateTime(new Date());
		article.setUpdateUser(ShiroUtil.getLoginName());
		String [] images = getParaValues("images");
		String imagesUrl = "";
		if(images!=null&&images.length>0){
			for (int i = 0; i < images.length; i++) {
				imagesUrl+=images[i]+",";
			}
		}
		article.setImages(imagesUrl);
		article.update();
		this.redirect("/admin/cmsarticle/list");
	}

	public void delete(){
		int articleId = getParaToInt("articleId");
		CmsArticle article = CmsArticle.dao.findById(articleId);
		article.delete();
		this.redirect("/admin/cmsarticle/list");
	}
}
